package com.example.Film.reviews.website.controllers;

import com.example.Film.reviews.website.dao.EvaluationRepository;
import com.example.Film.reviews.website.dao.MovieRepository;
import com.example.Film.reviews.website.dao.UserRepository;
import com.example.Film.reviews.website.entities.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("evaluations")
public class EvaluationController {

    @Autowired
    EvaluationRepository evaluationRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    MovieRepository movieRepository;

    @GetMapping
    public String displayEvaluation(Model model){
        List<Evaluation> evaluations= evaluationRepository.findAll();
        model.addAttribute("evaluations",evaluations);
        return "evaluation/list-evaluation";
    }
    @GetMapping("/new")
    public String displayEvaluationForm(Model model){
//        List<User> users= userRepository.findAll();
//       model.addAttribute("allUsers",users);
       List<Movie> movies = movieRepository.findAll();
       model.addAttribute("allMovies",movies);

//        User aUser=new User();
//       model.addAttribute("users",aUser);
        Evaluation aEvaluation = new Evaluation();
        model.addAttribute("evaluation",aEvaluation);
        return "evaluation/new-evaluation";
    }

    @PostMapping("/save")
    public String creatEvaluation(Model model,Evaluation evaluation){

        evaluationRepository.save(evaluation);
        return "redirect:/evaluations";
    }
}
